package com.mars.pay.controller;

import com.mars.pay.entity.Order;
import com.mars.pay.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 首页控制器
 *
 * @author Mars
 */
@Controller
@RequiredArgsConstructor
public class IndexController {

    private final OrderService orderService;

    /**
     * 首页（带分页订单列表）
     */
    @GetMapping("/")
    public String index(@RequestParam(defaultValue = "1") int page,
                        @RequestParam(defaultValue = "5") int size,
                        Model model) {
        // 查询订单列表（分页）
        List<Order> orders = orderService.listByPage(page, size);
        long total = orderService.getTotalCount();
        int totalPages = (int) Math.ceil((double) total / size);
        
        model.addAttribute("orders", orders);
        model.addAttribute("currentPage", page);
        model.addAttribute("pageSize", size);
        model.addAttribute("totalPages", totalPages);
        model.addAttribute("totalCount", total);
        
        return "index";
    }

    /**
     * 支付成功页
     */
    @GetMapping("/pay/success")
    public String paySuccess() {
        return "pay-success";
    }
}

